package com.hongdee.scm.inputinvoice.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.inputinvoice.entity.TScmInputInvoice;
import com.hongdee.scm.inputinvoice.entity.TScmInvoiceDetail;
import com.hongdee.scm.inputinvoice.service.ITScmInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Mr. Xu on 2017/5/12.
 */
@RestController
@RequestMapping("/scm/invoice")
public class TScmInvoiceController {
    @Autowired
    private ITScmInvoiceService itScmInvoiceService;

    /**
     * 发票分页查询
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {

        Map map = itScmInvoiceService.page(uid, start, limit, data);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 发票明细查询
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/pageDetail")
    @ResponseBody
    public Map pageDetail(@RequestParam(name = "uid", defaultValue = "") String uid,
                          @RequestParam(name = "token", defaultValue = "") String token,
                          @RequestParam(name = "start", defaultValue = "0") String start,
                          @RequestParam(name = "limit", defaultValue = "50") String limit,
                          @RequestParam(name = "vin", defaultValue = "") String vin,
                          @RequestParam(name = "id", defaultValue = "") String id) throws Exception {

        Map map = itScmInvoiceService.pageDetail(id, start, limit,vin);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 保存发票信息
     *
     * @param uid
     * @param token
     * @param data
     * @throws Exception
     */
    @RequestMapping("/saveInvoice")
    @ResponseBody
    public ResultEntity saveInvoice(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestParam(name = "type", defaultValue = "") String type,
                                    @RequestBody TScmInputInvoice data) throws Exception {
        ResultEntity res = new ResultEntity();
        itScmInvoiceService.saveorupdate(data, type, uid);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    /**
     * 发票查询详情
     *
     * @param uid
     * @param token
     * @throws Exception
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity infoInvoice(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestParam(name = "id", defaultValue = "") String id
    ) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = itScmInvoiceService.infoInvoice(id,uid);
        res.setSuccess(true);
        res.setMsg("查询成功！");
        res.setData(map);
        return res;
    }


    /**
     * 保存发票信息明细
     *
     * @param uid
     * @param token
     * @param data
     * @throws Exception
     */
    @RequestMapping("/saveInvoiceDetail")
    @ResponseBody
    public Map saveInvoiceDetail(@RequestParam(name = "uid", defaultValue = "") String uid,
                                          @RequestParam(name = "token", defaultValue = "") String token,
                                          @RequestParam(name = "id", defaultValue = "") String id,
                                          @RequestBody List<TScmInvoiceDetail> data) throws Exception {

        Map map = itScmInvoiceService.saveorupdateInvoiceDetail(data, uid, id);

        map.put("success", true);
        map.put("msg", "保存成功！");
        return map;
    }

    /**
     * 删除发票信息明细
     *
     * @param uid
     * @param token
     * @param id
     * @throws Exception
     */
    @RequestMapping("/delDetail")
    @ResponseBody
    public Map delDetail(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestParam(name = "iid", defaultValue = "") String iid,
                                  @RequestBody String[] id) throws Exception {

        Map map = itScmInvoiceService.delInvoiceDetail(id, iid,uid);
        map.put("success","true");
        map.put("msg","删除成功！");
        return map;
    }

    /**
     * 更新发票信息明细价格
     *
     * @param uid
     * @param token
     * @param id
     * @throws Exception
     */
    @RequestMapping("/updateDetail")
    @ResponseBody
    public Map updateDetail(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestParam(name = "iid", defaultValue = "") String iid,
                                  @RequestParam(name = "price", defaultValue = "") String price,
                                  @RequestBody String[] id) throws Exception {
        Map map = itScmInvoiceService.updatePrice(id, iid, price, uid);
        map.put("success","true");
        map.put("msg","更新成功！");
        return map;
    }

    /**
     * 删除红冲发票所选车辆
     *
     * @param uid
     * @param token
     * @param ids
     * @throws Exception
     */
    @RequestMapping("/delCars")
    @ResponseBody
    public ResultEntity delDetail(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestBody String[] ids) throws Exception {
        ResultEntity res = new ResultEntity();
        itScmInvoiceService.deleteCar(ids, uid);
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }

    /**
     * 查询历史发票价格调整明细
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/historyPage")
    @ResponseBody
    public Map historyPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "token", defaultValue = "") String token,
                           @RequestParam(name = "start", defaultValue = "0") String start,
                           @RequestParam(name = "limit", defaultValue = "50") String limit,
                           @RequestBody Map data) throws Exception {

        Map map = this.itScmInvoiceService.historyPage(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    //查询发票流水号
    @RequestMapping("/invoiceSerialNumber")
    @ResponseBody
    public Map invoiceSerialNumber(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token) throws Exception {

        Map map = new HashMap();
        String string = this.itScmInvoiceService.invoiceSerialNumber();
        map.put("serialNu", string);
        return map;
    }

    /**
     * 是否存在整冲红票
     * @param uid
     * @param token
     * @param invoiceNum
     * @return
     * @throws Exception
     */
    @RequestMapping("/existRedInvoice")
    @ResponseBody
    public Map existRedInvoice(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "invoiceNum", defaultValue = "") String invoiceNum) throws Exception {

        Map map = new HashMap();
        boolean status = this.itScmInvoiceService.existRedInvoice(invoiceNum);
        map.put("status", status);
        return map;
    }
    /**
     * 查询历史发票金额和目前发票金额
     * @param uid
     * @param token
     * @param vin
     * @return
     * @throws Exception
     */
    @RequestMapping("/historyInvoicePrice")
    @ResponseBody
    public Map historyInvoicePrice(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "vin", defaultValue = "") String vin) throws Exception {

        Map map = this.itScmInvoiceService.historyInvoicePrice(vin);
        return map;
    }
}
